package com.neolix.watch.domain;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

import static com.neolix.watch.NeolixApplication.getInstance;
import static java.util.Arrays.asList;

public enum WeatherType {

    UNKNOWN("未知", "none", 0) {
        @Override
        public int getDayBigPictureId() {
            if (getWeatherDescribe().contains("转")) {
                return findCommandType(getWeatherDescribe().substring(getWeatherDescribe().indexOf("转") + 1)).getDayBigPictureId();
            } else
                return getInstance().getResources().getIdentifier("ic_big_none", resType, getInstance().getPackageName());
        }

        @Override
        public int getNightBigPictureId() {
            if (getWeatherDescribe().contains("转")) {
                return findCommandType(getWeatherDescribe().substring(getWeatherDescribe().indexOf("转") + 1)).getNightBigPictureId();
            } else
                return getInstance().getResources().getIdentifier("ic_big_none", resType, getInstance().getPackageName());
        }

        @Override
        public int getDaySmallPictureId() {
            if (getWeatherDescribe().contains("转")) {
                return findCommandType(getWeatherDescribe().substring(getWeatherDescribe().indexOf("转") + 1)).getDaySmallPictureId();
            } else
                return getInstance().getResources().getIdentifier("ic_small_none", resType, getInstance().getPackageName());
        }

        @Override
        public int getNightSmallPictureId() {
            if (getWeatherDescribe().contains("转")) {
                return findCommandType(getWeatherDescribe().substring(getWeatherDescribe().indexOf("转") + 1)).getNightSmallPictureId();
            } else
                return getInstance().getResources().getIdentifier("ic_small_none", resType, getInstance().getPackageName());
        }
    },
    QING("晴", "qing", 2), DUOYUN("多云", "duoyun", 0), YIN("阴", "yin", 0),
    ZHENYU("阵雨", "zhenyu", 1), LEIZHENYU("雷阵雨", "leizhenyu", 1), LEIZHENYUBANBB("雷阵雨伴有冰雹", "leizhenyubanbb", 1),
    YUJIAXUE("雨夹雪", "yujiaxue", 1), XIAOYU("小雨", "xiaoyu", 1), ZHONGYU("中雨", "zhongyu", 1),
    DAYU("大雨", "dayu", 1), BAOYU("暴雨", "baoyu", 1), DABAOYU("大暴雨", "dabaoyu", 1),
    TEDABAOYU("特大暴雨", "tedabaoyu", 1), ZHENXUE("阵雪", "zhenxue", 1), XIAOXUE("小雪", "xiaoxue", 1),
    ZHONGXUE("中雪", "zhongxue", 1), DAXUE("大雪", "daxue", 1), BAOXUE("暴雪", "baoxue", 1),
    WU("雾", "wu", 0), DONGYU("冻雨", "dongyu", 1), SHANGCHENBAO("沙尘暴", "shachenbao", 0),
    YANGSHA("扬沙", "yangsha", 0), MAI("霾", "mai", 0), FUCHEN("浮尘", "yangsha", 0),
    QIANGSHACHENBAO("强沙尘暴", "shachenbao", 0),
    XIAOYUZHANZHONGYU("小雨转中雨", "zhongyu", 1), ZHONGYUZHUANDAYU("中雨转大雨", "dayu", 1), DAYUZHUANBAOYU("大雨转暴雨", "baoyu", 1),
    BAOYUZHUANDABAOYU("暴雨转大暴雨", "tedabaoyu", 1), DABAOYUZHUANTEDABAOYU("大暴雨转特大暴雨", "tedabaoyu", 1), XIAOXUEZHUANZHONGXUE("小雪转中雪", "zhongxue", 1),
    ZHONGXUEZHUANDAXUE("中雪转大雪", "daxue", 1), DAXUEZHUANBAOXUE("大雪转暴雪", "baoxue", 1);

    private static String resType = "drawable";

    public int getDayBigPictureId() {
        return getInstance().getResources().getIdentifier("ic_big_" + getPictureNameSuffix(), resType, getInstance().getPackageName());
    }

    public int getNightBigPictureId() {
        return getInstance().getResources().getIdentifier("ic_big_ye_" + getPictureNameSuffix(), resType, getInstance().getPackageName());
    }

    public int getDaySmallPictureId() {
        return getInstance().getResources().getIdentifier("ic_small_" + getPictureNameSuffix(), resType, getInstance().getPackageName());
    }

    public int getNightSmallPictureId() {
        return getInstance().getResources().getIdentifier("ic_small_ye_" + getPictureNameSuffix(), resType, getInstance().getPackageName());
    }

    private String weatherDescribe;
    private final String pictureNameSuffix;
    private int watchImageIndex;

    public int getWatchImageIndex() {
        return watchImageIndex;
    }

    public String getWeatherDescribe() {
        return weatherDescribe;
    }

    public void setWeatherDescribe(String describe) {
        this.weatherDescribe = describe;
    }

    public String getPictureNameSuffix() {
        return pictureNameSuffix;
    }

    private WeatherType(String describe, String pictureNameSuffix, int watchImageIndex) {
        this.weatherDescribe = describe;
        this.pictureNameSuffix = pictureNameSuffix;
        this.watchImageIndex = watchImageIndex;
    }

    public static WeatherType findCommandType(final String weatherDescribe) {
        Optional<WeatherType> weatherTypeOptional = Iterables.tryFind(asList(WeatherType.values()), new Predicate<WeatherType>() {
            @Override
            public boolean apply(WeatherType weatherType) {
                return weatherType.getWeatherDescribe().toString().equals(weatherDescribe.trim());
            }
        });

        WeatherType weatherType = weatherTypeOptional.isPresent() ? weatherTypeOptional.get() : UNKNOWN;
        if (weatherType == UNKNOWN)
            weatherType.setWeatherDescribe(weatherDescribe);
        return weatherType;
    }
}
